<?php

class Membership_model extends CI_Model {


    public function __construct(){
    $this->load->database();
    $this->load->library('session');
    	}
    
    function validate(){
        
    }
    
    function create_member(){
    $usnm =	$this->input->post('username');
    $new_user_data =array(
    	'kadi' => strtolower($this->input->post('username')),
    	'adi' => $this->input->post('name'),
    	'soyadi' => $this->input->post('lastname'),
    	'email' => $this->input->post('email'),
    	'sifre' => md5($this->input->post('pass')),
        'uyelik_tarihi' => date('d.m.y H:i'),
    	'takipci_say' => 0,
    	'tahmins_ort' => 0,
    	'tahmins_say' => 0,
    	'kazanan_tahmins' => 0,
    	'durum' => 0
    	);
    	$insert = $this->db->insert('tahminser',$new_user_data);
    	return $insert;
    	}
    	
    	function check_if_username_exists($username){
    	$this->db->where('kadi',$username);
    	$result = $this->db->get('tahminser');
    	if($result->num_rows() > 0){
    	    return FALSE;
    	}else{
    	    return TRUE;
    	}
    	}
        
        function check_if_email_exists($email){
    	$this->db->where('email',$email);
    	$result = $this->db->get('tahminser');
    	if($result->num_rows() > 0){
    	    return FALSE;
    	}else{
    	    return TRUE;
    	}
    	}
        
        function user_ctrl(){
        $username = $this->security->xss_clean($this->input->post('usernames'));
        $password = $this->security->xss_clean($this->input->post('passw'));
        
        if(filter_var($username,FILTER_VALIDATE_EMAIL)){
          $sonuc = $this->email_sign($username,$password);
          if($sonuc){
              return TRUE;
          }else {
              return FALSE;
          }
        }else {
            $que = $this->username_sign($username,$password);
            if($que){
              return TRUE;
            }else {
              return FALSE;
          }
        }
        } 
        function email_sign($user,$pass){
            $pass_md5 = md5($pass);
            $this->db->where('email',$user);
            $this->db->where('sifre',$pass_md5);
            $query = $this->db->get('tahminser');
            if($query->num_rows == 1){
                $row = $query->row();
                
                $data = array(
                    'id' => $row->id,
                    'kadi' => $row->kadi,
                    'email' => $row->email,
                    'adi' => $row->adi,
                    'soyadi' => $row->soyadi,
                    'takipci_say' => $row->takipci_say,
                    'tahmins_ort' => $row->tahmins_ort,
                    'durum' => $row->durum,
                    'validated' => TRUE
                );
                $this->session->set_userdata($data);
                return TRUE;
        }else {
            return FALSE;
        }
        }
        
       function username_sign($user,$pass){
          $pass_md5 = md5($pass);
          $this->db->where('kadi',$user);
          $this->db->where('sifre',$pass_md5);
          $query = $this->db->get('tahminser');
          if($query->num_rows == 1){
                $row = $query->row();
                $data = array(
                    'id' => $row->id,
                    'kadi' => $row->kadi,
                    'email' => $row->email,
                    'adi' => $row->adi,
                    'soyadi' => $row->soyadi,
                    'takipci_say' => $row->takipci_say,
                    'tahmins_ort' => $row->tahmins_ort,
                    'durum' => $row->durum,
                    'validated' => TRUE
                );
                $this->session->set_userdata($data);
                return TRUE;
        }else {
            return FALSE;
        }
       }
       
       function getAllTahmins($id){
           if($this->session->userdata('validated')){
           $this->db->where('tahminser_id',$id);
           $this->db->where('onay',1);
           $que = $this->db->get('kuponlar');
           return $que->result();}
           else {
               redirect('/login/loginn');
           }
       }
       
       function pass_ctrl($id){
            if($this->session->userdata('validated')){
                $current = $this->security->xss_clean($this->input->post('current'));
                $passer = $this->security->xss_clean($this->input->post('passwrd'));
                
                $curr_md5 = md5($current);
                $this->db->where('sifre',$curr_md5);
                $this->db->where('id',$id);
                $query = $this->db->get('tahminser');
            if($query->num_rows == 1){
                $this->update_pass($id, md5($passer));
                return TRUE;
        }else {
            return FALSE;
        }
            }else {
                redirect('/user/changePass');
            }
            
       }
       
       function update_pass($id,$passers){
          if($this->session->userdata('validated')){
            $datas=array('sifre'=>$passers);
            $this->db->where('id',$id);
            $this->db->update('tahminser',$datas); 
          } else{
              redirect('/login/loginn');
          }
       }
       
       
    
}
?>
